package com.rbtnet.blog.core.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rbtnet.blog.core.mapper.UserMapper;
import com.rbtnet.blog.core.model.User;
import com.rbtnet.blog.core.model.UserExample;
import com.rbtnet.blog.core.model.UserExample.Criteria;
import com.rbtnet.blog.core.service.UserService;

/**
 * 
 * @author 1136009436@qq.com
 *
 */
@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserMapper userMapper;

	@Override
	public User login(User user) {
		UserExample example = new UserExample();
		Criteria c = example.createCriteria();
		c.andUsernameEqualTo(user.getUsername());
		c.andPasswordEqualTo(user.getPassword());
		List<User> list = userMapper.selectByExample(example);
		if(list == null || list.size() == 0){
			return null;
		}
		return list.get(0);
	}

	@Override
	public Map<String, Object> findAllUser(int pageNum, int pageSize) {
		Map<String, Object> mapResult = new HashMap<String, Object>();
		PageHelper.startPage(pageNum, pageSize);
		UserExample example = new UserExample();
		List<User> list = userMapper.selectByExample(example);
		mapResult.put("totalPage", ((Page)list).getPages());
		mapResult.put("data", list);
		return mapResult;
	}

}
